//
//  NewsCommentViewController.m
//  suRead
//
//  Created by 李 诗洋 on 15/11/13.
//  Copyright © 2015年 bao. All rights reserved.
//

#import "NewsCommentViewController.h"
#import "NewsCommentCell.h"
#import "Masonry.h"
#import "UIControl+Blocks.h"
#import "ServiceUserNew.h"
#import "M_Comment.h"
#import "NewsSendCommentsViewController.h"
#import "UIViewController+HUD.h"

@interface NewsCommentViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic,weak) IBOutlet UITableView *newsCommentTableView;
@property (nonatomic,strong)NSMutableArray *commentArray;
@end

@implementation NewsCommentViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.newsCommentTableView.sectionFooterHeight = 0;
    [self.newsCommentTableView registerNib:[UINib nibWithNibName:@"NewsCommentCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"NewsCommentCell"];
    [self showHUDProgress:@""];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:YES];
    
    __weak typeof(self) weak_self = self;
    [ServiceUserNew doGetComments:self.article_id success:^(id  _Nonnull responseObject, NSString * _Nonnull message) {
        self.commentArray = [NSMutableArray new];
        [self.commentArray addObjectsFromArray:responseObject];
        [self.newsCommentTableView reloadData];
        [weak_self removeHUDProgress];
    } failure:^(NSString * _Nonnull message) {
        [weak_self removeHUDProgress];
        [weak_self showHUDText:message];
    }];
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 10.0f;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return [self.commentArray count];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    M_Comment *commentData = [self.commentArray objectAtIndex:indexPath.section];
    return [NewsCommentCell heightWithContent:commentData.f_content];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NewsCommentCell *cell = (NewsCommentCell *)[tableView dequeueReusableCellWithIdentifier:@"NewsCommentCell"];
    M_Comment *commentData = [self.commentArray objectAtIndex:indexPath.section];
    [cell setCellData:commentData];
    
    __weak typeof(self) weak_self = self;
    [cell setReplyClickCallBack:^(){//回复
        NewsSendCommentsViewController *controller = [NewsSendCommentsViewController new];
        controller.f_article_id = commentData.f_article_id;
        controller.f_pid        = commentData.f_pid;
        controller.f_to_id      = commentData.f_to_id;
        controller.isReply     = YES;
        [weak_self.navigationController pushViewController:controller animated:YES];
    }];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

- (IBAction)writeComment:(id)sender {
    //写评论
    NewsSendCommentsViewController *controller = [NewsSendCommentsViewController new];
    controller.f_article_id = self.article_id;
    [self.navigationController pushViewController:controller animated:YES];
}

- (IBAction)back:(id)sender
{
    [self.navigationController popViewControllerAnimated:YES];
}

@end
